<%--
  Created by IntelliJ IDEA.
  User: You
  Date: 2020/5/20
  Time: 11:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" import="java.util.*,com.xingyun.info_manager.entity.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-lg12">
            <fieldset class="layui-elem-field">
                <legend>编辑用户</legend>
                <div class="layui-field-box">
                    <form id="formAdd" class="layui-form " action="">
                        <input type="hidden" id="userId" name="userId" value="${adminUser.userId}" >
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-inline" style="width: 180px;">
                                    <input type="text" id="userName" name="userName" value="${adminUser.userName}" lay-verify="required" placeholder="请输入用户名"  class="layui-input">
                                </div>
                                <%--<div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">密&nbsp;&nbsp;码</label>
                                <div class="layui-input-inline" style="width: 180px;">
                                    <input type="password" lay-verify="required" name="userPwd" value="${adminUser.userPwd}" placeholder="请输入密码" autocomplete="off" class="layui-input">
                                </div>
                                <%--<div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">真实姓名</label>
                                <div class="layui-input-inline" style="width: 180px;">
                                    <input type="text" name="realName" value="${adminUser.realName}" placeholder="请输入真实姓名"  class="layui-input">
                                </div>
                                <%--<div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
                            </div>

                            <div class="layui-inline">
                                <label class="layui-form-label">手机号</label>
                                <div class="layui-input-inline" style="width: 180px;">
                                    <input type="text" name="userMobile" lay-verify="phone" value="${adminUser.userMobile}" placeholder="请输入手机号"  class="layui-input">
                                </div>
                                <%--<div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">渠&nbsp;&nbsp;道</label>
                                <div class="layui-input-inline" style="width: 180px;">
                                    <select id="channelId" name="channelId" lay-verify="required">
                                        <c:choose>
                                            <c:when test="${admin.channelId == 0}">

                                                <option value="1" <c:if test="${adminUser.channelId == 1}">selected</c:if>>院内渠道</option>

                                                <option value="2" <c:if test="${adminUser.channelId == 2}">selected</c:if>>市场渠道</option>
                                                <option value="3" <c:if test="${adminUser.channelId == 3}">selected</c:if>>网络渠道</option>
                                            </c:when>
                                            <c:otherwise>
                                                <option value="${adminUser.channelId}">${adminUser.channelName}</option>
                                            </c:otherwise>
                                        </c:choose>
                                    </select>
                                </div>
                                <%--<div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">角色权限</label>
                                <div class="layui-input-inline" style="width: 180px;">
                                    <select name="roleId" lay-verify="required">
                                        <option value="3" <c:if test="${adminUser.roleId==3}">selected</c:if>>个人账户</option>
                                        <option value="2" <c:if test="${adminUser.roleId==2}">selected</c:if>>渠道管理员</option>
                                        <option value="1" disabled>全渠道管理员</option>
                                    </select>
                                </div>
                                <%--<div class="layui-form-mid layui-word-aux">辅助文字</div>--%>
                            </div>
                        </div>
                        <div class="layui-form-item" style="margin-left: 265px;">
                            <ul >
                                <li style="float:left;">
                                    <div class="" style="">
                                        <button id="commitBtn" class="layui-btn" lay-submit lay-filter="formDemo">提交</button>
                                    </div>
                                </li>
                                <li style="float:left;margin-left: 25px">
                                    <div class="" style="">
                                        <button type="button" id="backBtn" class="layui-btn layui-btn-primary">返回</button>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>
            </fieldset>
        </div>
    </div>
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/javascript/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/layui/layui.all.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/javascript/dw_form_submit.js"></script>
<script>
    var form = layui.form;
    // 自定义校验规则
    //这个里面的title、number就写在Html 代码中lay-verify的属性值，即可
    //例如：lay-verify="title"
    // form.verify({
    //     title : function(value) {
    //         if (value.length < 5) {
    //             return '至少输入5个字符啊';
    //         }
    //     },
    //     number : [/^[0-9]*$/, '必须输入数字啊']
    // });


    // 监听提交
    form.on('submit(formDemo)', function(data) {
        var userId = $("#userId").val();
        var userName = $("#userName").val();
        var channelId = $("#channelId").val();
        $.ajax({
            url : '${pageContext.request.contextPath}/admin/isExist?userId=' + userId ,//数据接口
            type : 'post',
            contentType: "application/x-www-form-urlencoded",
            dataType : 'json',
            data: {"userName":userName, "channelId":channelId},//转化json格式
            success : function(data) {
                if(data.code == 1){
                    //弹出框提示
                    layer.msg(data.msg);
                } else {
                    updateSubmit();
                }
            }
        });
        return false;
    });

    function updateSubmit(){
        $.ajax({
            url : '${pageContext.request.contextPath}/admin/update',//数据接口
            type : 'post',
            contentType: "application/x-www-form-urlencoded",
            dataType : 'json',
            data: $('#formAdd').serialize(),//转化json格式
            success : function(data) {
                if(data.code == 0){
                    //弹出框提示
                    layer.msg("用户修改成功", {
                        area: '200px',
                        time: 0, //不自动关闭
                        btn: ['确定'],
                        btnAlign: 'c', //按钮居中
                        yes: function(index, layero){
                            //按钮【按钮一】的回调
                            //当你在iframe页面关闭自身时，关闭新增弹出框
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        }
                    });
                } else {
                    layer.msg(data.msg, {
                        area: '200px',
                        time: 5000, //5s后自动关闭
                        btn: ['确定'],
                        btnAlign: 'c', //按钮居中
                    });
                }
            },
            error:function (data) {
                layer.msg("修改失败", {
                    area: '200px',
                    time: 5000, //5s后自动关闭
                    btn: ['确定'],
                    btnAlign: 'c', //按钮居中
                });
            }
        });
    }
    // form.render(); // 更新全部

    <%--修改提交按钮--%>
     <%--$("#commitBtn").click(function () {--%>
         <%--/**--%>
          <%--* 通用表单验证--%>
          <%--*/--%>
         <%--form.verify();--%>

         <%--$.ajax({--%>
             <%--url : '${pageContext.request.contextPath}/admin/update',//数据接口--%>
             <%--type : 'post',--%>
             <%--contentType: "application/x-www-form-urlencoded",--%>
             <%--dataType : 'json',--%>
             <%--data: $('#formAdd').serialize(),//转化json格式--%>
             <%--success : function(data) {--%>
                 <%--if(data.code == 0){--%>
                     <%--//弹出框提示--%>
                     <%--layer.msg("用户修改成功", {--%>
                         <%--area: '200px',--%>
                         <%--time: 0, //不自动关闭--%>
                         <%--btn: ['确定'],--%>
                         <%--btnAlign: 'c', //按钮居中--%>
                         <%--yes: function(index, layero){--%>
                             <%--//按钮【按钮一】的回调--%>
                             <%--//当你在iframe页面关闭自身时，关闭新增弹出框--%>
                             <%--var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引--%>
                             <%--parent.layer.close(index); //再执行关闭--%>
                         <%--}--%>
                     <%--});--%>
                 <%--} else {--%>
                     <%--layer.msg(data.msg, {--%>
                         <%--area: '200px',--%>
                         <%--time: 5000, //5s后自动关闭--%>
                         <%--btn: ['确定'],--%>
                         <%--btnAlign: 'c', //按钮居中--%>
                     <%--});--%>
                 <%--}--%>
             <%--},--%>
             <%--error:function (data) {--%>
                 <%--layer.msg("修改失败", {--%>
                     <%--area: '200px',--%>
                     <%--time: 5000, //5s后自动关闭--%>
                     <%--btn: ['确定'],--%>
                     <%--btnAlign: 'c', //按钮居中--%>
                 <%--});--%>
             <%--}--%>
         <%--});--%>
     <%--})--%>

    /*关闭按钮*/
    $("#backBtn").click(function () {
        //当你在iframe页面关闭自身时，关闭新增弹出框
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    })
</script>
</body>
</html>
